package com.yuxi;

/**
 * Created by yuxi on 17/2/3.
 */
public class DoubleCheckedVolatile {
    //加上volatile可以保证可见性,也就是说线程a对doubleChecked操作对线程b来说是可见的
    //volatile的本质还是禁止指令重排序
    private  volatile static DoubleCheckedVolatile doubleChecked;

    private DoubleCheckedVolatile() {
    }

    public static DoubleCheckedVolatile getInstance() {
        if (doubleChecked == null) {//第一次检查
            synchronized (DoubleCheckedVolatile.class) {//加锁
                if (doubleChecked == null) {//再次检查
                    doubleChecked = new DoubleCheckedVolatile();//创建对象,问题的根源
                }
            }
        }
        return doubleChecked;
    }
}
